<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品详情" prop="productDetail">
        <el-input
          v-model="queryParams.productDetail"
          placeholder="请输入商品详情"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品类别" prop="categoryId">
        <el-select v-model="queryParams.categoryId" placeholder="请选择类别" label="categoryId">
          <el-option
            v-for="dict in dictionariesList"
            :key="dict.categoryId"
            :label="dict.categoryName"
            :value="dict.categoryId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="大份价格" prop="max_price">
        <el-input
        v-model="queryParams.max_price"
        placeholder="请输入大份价格"
        clearable
        @keyup.enter.native="handleQuery">
        </el-input>
      </el-form-item>
      <el-form-item label="中份价格" prop="price">
        <el-input
        v-model="queryParams.price"
        placeholder="请输入中份价格"
        clearable
        @keyup.enter.native="handleQuery">
        </el-input>
      </el-form-item>
      <el-form-item label="小份价格" prop="min_price">
        <el-input
        v-model="queryParams.min_price"
        placeholder="请输入小份价格"
        clearable
        @keyup.enter.native="handleQuery">
        </el-input>
      </el-form-item>
      <!-- <el-form-item label="规格ID" prop="specificationId">
        <el-input
          v-model="queryParams.specificationId"
          placeholder="请输入规格ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="销量" prop="saleNum">
        <el-input
          v-model="queryParams.saleNum"
          placeholder="请输入销量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否折扣" prop="isDiscount">
        <el-select v-model="queryParams.isDiscount" placeholder="选择是否折扣" label="1">
          <el-option label="无" :value="null"></el-option>
          <el-option label="是" :value="1"></el-option>
          <el-option label="否" :value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-table-column label="上下架状态" align="center">
  <template slot-scope="scope">
    {{ scope.row.isOnSale }}
  </template>
</el-table-column>
      <el-form-item label="折扣力度" prop="discontStrength">
        <el-input
          v-model="queryParams.discontStrength"
          placeholder="请输入折扣力度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="店铺id" prop="storeId">
        <el-input
          v-model="queryParams.storeId"
          placeholder="请输入店铺id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:product:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:product:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:product:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:product:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="productList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商品ID" align="center" prop="productId" />
      <el-table-column label="商品名称" align="center" prop="productName" />
      <el-table-column label="商品图片" align="center" prop="productPhoto" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.productPhoto" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="商品详情" align="center" prop="productDetail" />
      <el-table-column label="大份价格" align="center" prop="max_price" />
      <el-table-column label="中份价格" align="center" prop="price" />
      <el-table-column label="小份价格" align="center" prop="min_price" />
      <el-table-column label="类别" align="center" prop="categoryName" />
      <!-- <el-table-column label="规格ID" align="center" prop="specificationId" /> -->
      <el-table-column label="销量" align="center" prop="saleNum" />
      <el-table-column label="是否折扣" align="center" prop="isDiscount" />
      <el-table-column label="上下架状态" align="center" prop="isOnSale" />
      <el-table-column label="折扣力度" align="center" prop="discontStrength" />
      <el-table-column label="店铺id" align="center" prop="storeId" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:product:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:product:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品信息 ：与店铺关联对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商品名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="商品图片" prop="productPhoto">
          <image-upload v-model="form.productPhoto"/>
        </el-form-item>
        <el-form-item label="商品详情" prop="productDetail">
          <el-input v-model="form.productDetail" placeholder="请输入商品详情" />
        </el-form-item>
        <!-- <el-form-item label="商品价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入价格" />
        </el-form-item> -->
        <el-form-item label="商品类别" prop="categoryId">
        <el-select v-model="form.categoryId" placeholder="请选择类别" label="categoryId">
          <el-option
            v-for="dict in dictionariesList"
            :key="dict.categoryId"
            :label="dict.categoryName"
            :value="dict.categoryId">
          </el-option>
        </el-select>
      </el-form-item>
        <!-- <el-form-item label="规格ID" prop="specificationId">
          <el-input v-model="form.specificationId" placeholder="请输入规格ID" />
        </el-form-item> -->
        <el-form-item label="大份价格" prop="max_price">
          <el-input v-model="form.max_price" placeholder="请输入大份价格"></el-input>
        </el-form-item>
        <el-form-item label="中份价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入中份价格"></el-input>
        </el-form-item>
        <el-form-item label="小份价格" prop="min_price">
          <el-input v-model="form.min_price" placeholder="请输入小份价格"></el-input>
        </el-form-item>
        <!-- <el-form-item label="销量" prop="saleNum">
          <el-input v-model="form.saleNum" placeholder="请输入销量" />
        </el-form-item> -->
        <el-form-item label="是否打折" prop="isDiscount">
          <el-select v-model="form.isDiscount" placeholder="选择是否折扣" label="1111">
            <el-option label="是" :value="1" />
            <el-option label="否" :value="0" />
          </el-select>
        </el-form-item>
        <el-form-item label="折扣力度" prop="discontStrength">
          <el-input v-model="form.discontStrength" placeholder="请输入销量" />
        </el-form-item>
        <el-form-item label="上下架状态" prop="isOnSale">
          <el-select v-model="form.isOnSale" placeholder="请选择上下架状态" label="1111">
            <el-option label="上架" :value="1" />
            <el-option label="下架" :value="0" />
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="店铺id" prop="storeId">
          <el-input v-model="form.storeId" placeholder="请输入店铺id" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProduct, getProduct, delProduct, addProduct, updateProduct,listDictionaries } from "@/api/system/product";

export default {
  name: "Product",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品信息 ：与店铺关联表格数据
      productList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 商品类别字典
      dictionariesList:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productName: null,
        productPhoto: null,
        productDetail: null,
        categoryId: null,
        specificationId: null,
        saleNum: null,
        isDiscount: null,
        isOnSale: null,
        discontStrength: null,
        storeId: null,
        price: null,
        max_price:null,
        min_price:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        productName: [
          { required: true, message: "商品名称不能为空", trigger: "blur" }
        ],
        storeId:[
          { required:true,message:"店铺不能为空", trigger:"blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDictionaries();
  },
  
  methods: {





    //查询类别字典
    getDictionaries(){
      listDictionaries().then(response => {
        // console.log(response)  
        this.dictionariesList = response.rows
        console.log(this.dictionariesList)
      })
    },




    /** 查询商品信息 ：与店铺关联列表 */

    getList() {
      this.loading = true;
      listProduct(this.queryParams).then(response => {
        this.productList = response.rows.map(item => {
        // 确保 isOnSale 的值是数字类型
        const isOnSaleValue = parseInt(item.isOnSale, 10);
        const isDiscountValue = parseInt(item.isDiscount, 10);
        // 将 isOnSale 的数值转换为“上架”或“下架”的字符串
        item.isOnSale = isOnSaleValue === 1 ? '上架' : '下架';
        item.isDiscount = isDiscountValue === 1 ? '打折' : '否';
        return item;
        });
      this.total = response.total;
      this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        productId: null,
        productName: null,
        productPhoto: null,
        productDetail: null,
        categoryId: null,
        specificationId: null,
        saleNum: null,
        isDiscount: null,
        isOnSale: null,
        discontStrength: null,
        storeId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.productId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品信息 ：与店铺关联";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const productId = row.productId || this.ids
      getProduct(productId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商品信息 ：与店铺关联";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.productId != null) {
            updateProduct(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProduct(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
            // addProductInventory(this.form).then(response => {
            //   this.$modal.msgSuccess("新增成功");
            //   consolel.log("新增库存成功")
            // })
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const productIds = row.productId || this.ids;
      const storeId = row.storeId || this.ids;
      this.$modal.confirm('是否确认删除商品信息 ：与店铺关联编号为"' + productIds + '"的数据项？').then(function() {
        return delProduct(productIds,storeId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/product/export', {
        ...this.queryParams
      }, `product_${new Date().getTime()}.xlsx`)
    },
  }
};
</script>
